package org.jcb;



import org.jcb.model.ChineseQuestionnaire;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class ChiniseView extends View {
Paint mPiePaint ;
Paint paintQuestion ;
Paint paintTextAnswer, paintTextAnswerTrans ;
//Question question ;
Circle circleGreen ;
Circle circleRed ;
float translationSize = 1 ;
int shift = 0 ;
int state ;
private int STATE_QUESTION = 0 ;
private int STATE_ANSWER = 1 ;
private int xQuestionText, yQuestionText, yAnswerText,yAnswerTranslateText  ;	
boolean screenConfigAdjusted = false ;
final ChinoisActivity chinoisActivity ;
ChineseQuestionnaire questionnaire ;



public ChineseQuestionnaire getQuestionnaire() {
	return questionnaire;
}


public void setQuestionnaire(ChineseQuestionnaire questionnaire) {
	this.questionnaire = questionnaire;
}

public boolean isScreenConfigAdjusted() {
	return screenConfigAdjusted;
}


public void setScreenConfigAdjusted(boolean screenConfigAdjusted) {
	this.screenConfigAdjusted = screenConfigAdjusted;
}


public ChiniseView (ChinoisActivity chinoisActivity) {
	super(chinoisActivity);
	this.chinoisActivity = chinoisActivity ;
	OnTouchListener touchListener = new OnTouchListener() {
		public boolean onTouch(View arg0, MotionEvent me) {
			Log.i("", ",,,ùùùon touch");
			
			
			if (userSelectAnswer(me.getX(),me.getY() )) {
				Log.i("", "select answer");
				state = STATE_ANSWER ;
			}
			else if (userSelectNext(me.getX(),me.getY() )) {
				//question.nextQuestion();
				try {
					questionnaire.nextQuestion();
				} catch (Exception e) {
					showResult();			
				}
				state = STATE_QUESTION ;
			}
			
			arg0.invalidate();
			return false ;
		}
    };        
	
    this.setOnTouchListener(touchListener); 
    //question = new Question();	
	mPiePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	
	
}

public void showResult() {
	chinoisActivity.showResult();
}
	
public boolean userSelectAnswer(float x, float y) {
	return circleRed.inside(new Point((int) x, (int) y)) ;
}

public boolean userSelectNext(float x, float y) {
	return circleGreen.inside(new Point((int) x, (int) y)) ;
}

public int getWidth(Canvas canvas) {
	return canvas.getWidth() ;
}

public int getHeight(Canvas canvas) {
	return canvas.getHeight() ;
}

public void drawLimit(Canvas canvas) {
	mPiePaint.setColor(Color.BLUE);
	int leftLimit = 10 ;
	int rightLimit = getWidth() -  leftLimit  ;
	int topLimit = 10 ;
	int bottomLimit = getHeight() -  topLimit  ;	
	canvas.drawRect(10, 10, rightLimit, bottomLimit, mPiePaint) ;
}

public int t(int x) {
	return shift + (int) (translationSize * x ) ; 
}


public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    
    if (! screenConfigAdjusted) {
    	//cercle : 7.5 % hauteur ecran, x à width - 1.5 radius, y a milieu - 3 radius
    	//pour h = 480 radius = 60 font = 121
    	//pour w=480
    	//
    	
    	Log.i("", "canvas.getHeight()=" + canvas.getHeight());
    	Log.i("", "canvas.getWidth()=" + canvas.getWidth());
    	translationSize = canvas.getHeight() / 800f ;
    	Log.i("", "translationSize=" + translationSize);
    	    	
    	int radiusCircle = (int) (60 * translationSize) ;
    	int xCircle = canvas.getWidth() - (int) (1.5 * radiusCircle) ;
    	int yCircleHaut = (canvas.getHeight() / 2 ) - (int) (3 * radiusCircle) ;
    	int yCircleBas =  yCircleHaut + (int) (2.5 * radiusCircle) ;
    	
    	circleGreen = new Circle(xCircle, yCircleHaut , radiusCircle) ;
    	circleRed = new Circle(xCircle, yCircleBas , radiusCircle) ;
    	
    	//determination taille
    	paintQuestion = new Paint();
    	paintQuestion.setAntiAlias(true);
        
        paintQuestion.setColor(Color.WHITE);
        paintQuestion.setTextAlign(Paint.Align.LEFT);
        float sizeFont = 121 * translationSize ;
        paintQuestion.setTextSize(sizeFont);
       
        
        yQuestionText = (int) (120 * translationSize) ;
        Log.i("", "yQuestionText=" + yQuestionText);
        xQuestionText = (int) (40 * translationSize) ; ;
        
        //answser
        paintTextAnswer = new Paint();
        paintTextAnswer.setAntiAlias(true);
        paintTextAnswer.setTextSize(70 * translationSize);
        paintTextAnswer.setColor(Color.WHITE);
        paintTextAnswer.setTextAlign(Paint.Align.LEFT);            
        yAnswerText = canvas.getHeight() / 2 + (int) (120 *  translationSize) ;
        yAnswerTranslateText = yAnswerText + (int) (50 *  translationSize) ;
        screenConfigAdjusted = true ;
        
        paintTextAnswerTrans = new Paint();
        paintTextAnswerTrans.setTextSize(50 * translationSize);
        paintTextAnswerTrans.setColor(Color.WHITE);
        paintTextAnswerTrans.setTextAlign(Paint.Align.LEFT);            
        
        
    }
    
    drawLimit(canvas);
    
    
    Log.i("",questionnaire.getChineseQuestion());
    canvas.drawText(questionnaire.getChineseQuestion(), xQuestionText, yQuestionText, paintQuestion);    

    Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    circlePaint.setColor(Color.GREEN);
    canvas.drawCircle(circleGreen.x, circleGreen.y , circleGreen.radius, circlePaint);
    circlePaint.setColor(Color.RED);
    canvas.drawCircle(circleRed.x, circleRed.y , circleRed.radius, circlePaint);

    if (state == STATE_ANSWER) {	
    	canvas.drawText(questionnaire.getPinyin(), xQuestionText, yAnswerText, paintTextAnswer);
    	canvas.drawText(questionnaire.getTranslation(), xQuestionText, yAnswerTranslateText, paintTextAnswerTrans);
    }
    
    

}



}
